package com.tencreat.mall.controller.admin;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tencreat.common.annotation.Log;
import com.tencreat.common.core.HttpResult;
import com.tencreat.common.core.controller.BaseController;
import com.tencreat.common.core.domain.AjaxResult;
import com.tencreat.common.core.domain.model.ReqIdParam;
import com.tencreat.common.core.domain.model.ReqIdsParam;
import com.tencreat.common.enums.BusinessType;
import com.tencreat.common.utils.poi.ExcelUtil;
import com.tencreat.domain.account.AccountWithdrawInfo;
import com.tencreat.domain.account.MallUser;
import com.tencreat.domain.account.dto.AdminAuditResultDTO;
import com.tencreat.domain.mall.SupplierWithdrawAudit;
import com.tencreat.domain.mall.vo.AuditResultVO;
import com.tencreat.domain.mall.vo.RrsAuditResultVO;
import com.tencreat.service.account.IAccountWithdrawInfoService;
import com.tencreat.service.account.IMallUserService;
import com.tencreat.service.account.ISupplierWithdrawAuditService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 供应商提现记录Controller
 *
 * @author tencreat
 * @date 2022-03-17
 */
@Api(tags="供应商提现记录api")
@RestController
@RequestMapping("/mall/supplierAudit")
public class AdminSupplierWithdrawAuditController extends BaseController {
    @Autowired
    private ISupplierWithdrawAuditService supplierWithdrawAuditService;
    @Resource
    private IAccountWithdrawInfoService withdrawInfoService;
    @Resource
    private IMallUserService mallUserService;
    /**
     * 查询供应商提现记录列表
     */
    @ApiOperation(value="查询供应商提现记录列表")
    @PreAuthorize("@ss.hasPermi('system:audit:list')")
    @PostMapping("/list")
    public HttpResult<IPage<SupplierWithdrawAudit>> list(@RequestBody  SupplierWithdrawAudit supplierWithdrawAudit) {
        QueryWrapper<SupplierWithdrawAudit> wrapper = Wrappers.query(supplierWithdrawAudit);
        Page<SupplierWithdrawAudit> page =  new Page<>(supplierWithdrawAudit.getPageNum(), supplierWithdrawAudit.getPageSize());

        wrapper.eq(supplierWithdrawAudit.getUuid() != null,"uuid",supplierWithdrawAudit.getUuid());//商家id
        wrapper.eq(supplierWithdrawAudit.getMobile() != null,"mobile",supplierWithdrawAudit.getMobile());//手机号
        wrapper.eq(supplierWithdrawAudit.getName() != null,"name",supplierWithdrawAudit.getName());//商家名称
        wrapper.eq(supplierWithdrawAudit.getWithdrawType() != null,"withdraw_type",supplierWithdrawAudit.getWithdrawType());//提现方式
        wrapper.ge(supplierWithdrawAudit.getResult() != null, "result", supplierWithdrawAudit.getResult());//审核状态
        //创建时间
        wrapper.ge(supplierWithdrawAudit.getStartTime() != null, "create_time", supplierWithdrawAudit.getStartTime());
        wrapper.ge(supplierWithdrawAudit.getEndTime() != null, "create_time", supplierWithdrawAudit.getEndTime());
        //审核时间
        wrapper.ge(supplierWithdrawAudit.getAuditStartTime() != null, "audit_time", supplierWithdrawAudit.getAuditStartTime());
        wrapper.ge(supplierWithdrawAudit.getAuditEndTime() != null, "audit_time", supplierWithdrawAudit.getAuditEndTime());
        wrapper.orderByDesc("result=0").orderByDesc("create_time");
        return HttpResult.ok(supplierWithdrawAuditService.page(page, wrapper));
    }

    /**
     * 导出供应商提现记录列表
     */
    @ApiOperation(value="导出供应商提现记录列表")
    @PreAuthorize("@ss.hasPermi('system:audit:export')")
    @Log(title = "财务管理>提现审核-货款", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public AjaxResult export(@RequestBody SupplierWithdrawAudit supplierWithdrawAudit) {
        QueryWrapper<SupplierWithdrawAudit> wrapper = Wrappers.query(supplierWithdrawAudit);
        Page<SupplierWithdrawAudit> page =  new Page<>(1, Integer.MAX_VALUE);
        wrapper.orderByDesc("result=0").orderByDesc("create_time");
        wrapper.ge(supplierWithdrawAudit.getStartTime() != null, "create_time", supplierWithdrawAudit.getStartTime());
        wrapper.le(supplierWithdrawAudit.getEndTime() != null, "create_time", supplierWithdrawAudit.getEndTime());
        List<SupplierWithdrawAudit> list = supplierWithdrawAuditService.page(page, wrapper).getRecords();
        list.forEach(item ->{
            item.setCreateTime2(item.getCreateTime());
        });
        ExcelUtil<SupplierWithdrawAudit> util = new ExcelUtil<SupplierWithdrawAudit>(SupplierWithdrawAudit.class);
        return util.exportExcel(list, "audit");
    }

    /**
     * 获取供应商提现记录详细信息
     */
    @ApiOperation(value="获取供应商提现记录详细信息")
    @PreAuthorize("@ss.hasPermi('system:audit:query')")
    @PostMapping(value = "/getInfo")
    public HttpResult<SupplierWithdrawAudit> getInfo(@RequestBody  ReqIdParam id) {
        SupplierWithdrawAudit supplierWithdrawAudit = supplierWithdrawAuditService.selectSupplierWithdrawAuditById(id);
        String mobile = supplierWithdrawAudit.getMobile();
        MallUser user = mallUserService.lambdaQuery().eq(MallUser::getMobile, mobile).one();
        if(user == null ){
            return HttpResult.ok(supplierWithdrawAudit);
        }
        AccountWithdrawInfo accountWithdrawInfo = withdrawInfoService.lambdaQuery().eq(AccountWithdrawInfo::getUserId, user.getUuid()).oneOpt().orElse(new AccountWithdrawInfo());
        String phone = "";
        String name = "";
        String bankName = "";
        if (supplierWithdrawAudit.getWithdrawType() == 1) {
            phone = accountWithdrawInfo.getWechatPhone();
            name = accountWithdrawInfo.getWechatName();
            supplierWithdrawAudit.setQr(accountWithdrawInfo.getWechatQr());
        } else if (supplierWithdrawAudit.getWithdrawType() == 2) {
            phone = accountWithdrawInfo.getAlipayPhone();
            name = accountWithdrawInfo.getAlipayName();
            supplierWithdrawAudit.setQr(accountWithdrawInfo.getAlipayQr());
        } else {
            phone = accountWithdrawInfo.getBankCode();
            bankName = accountWithdrawInfo.getBankName();
            name = accountWithdrawInfo.getCardName();
        }
        supplierWithdrawAudit.setPhone(phone);
        supplierWithdrawAudit.setName(name);
        supplierWithdrawAudit.setBankName(bankName);
        return HttpResult.ok(supplierWithdrawAudit);
    }

    /**
     * 新增供应商提现记录
     */
    @ApiOperation(value="新增供应商提现记录")
    @PreAuthorize("@ss.hasPermi('system:audit:add')")
    @Log(title = "供应商提现记录", businessType = BusinessType.INSERT)
    @PostMapping(value = "/add")
    public HttpResult<Boolean> add(@RequestBody  SupplierWithdrawAudit supplierWithdrawAudit) {
        return to(supplierWithdrawAuditService.insertSupplierWithdrawAudit(supplierWithdrawAudit));
    }

    /**
     * 修改供应商提现记录
     */
    @ApiOperation(value="修改供应商提现记录")
    @PreAuthorize("@ss.hasPermi('system:audit:edit')")
    @Log(title = "供应商提现记录", businessType = BusinessType.UPDATE)
    @PostMapping(value = "/edit")
    public HttpResult<Boolean> edit(@RequestBody  SupplierWithdrawAudit supplierWithdrawAudit) {
        return to(supplierWithdrawAuditService.updateSupplierWithdrawAudit(supplierWithdrawAudit));
    }

    /**
     * 删除供应商提现记录
     */
    @ApiOperation(value="删除供应商提现记录")
    @PreAuthorize("@ss.hasPermi('system:audit:remove')")
    @Log(title = "供应商提现记录", businessType = BusinessType.DELETE)
	@PostMapping("/remove")
    public HttpResult<Boolean> remove(@RequestBody  ReqIdsParam ids) {
        return to(supplierWithdrawAuditService.deleteSupplierWithdrawAuditByIds(ids));
    }


    @ApiOperation(value = "审核")
    @Log(title = "财务管理>提现审核-货款", businessType = BusinessType.UPDATE)
    @PostMapping(value = "/auditResult")
    public HttpResult<Boolean> auditResult(@RequestBody AdminAuditResultDTO param) {
        supplierWithdrawAuditService.auditResult(param);
        return to(true);
    }

    @ApiOperation(value = "金额统计")
    @PostMapping(value = "/statistics")
    public HttpResult<RrsAuditResultVO> statistics() {
        RrsAuditResultVO result = supplierWithdrawAuditService.priceStatistics();
        return HttpResult.ok(result);
    }

    @ApiOperation(value = "金额统计(新)")
    @GetMapping(value = "/statisticsV2")
    public HttpResult<AuditResultVO> statisticsV2() {
        AuditResultVO result = supplierWithdrawAuditService.priceStatisticsV2();
        return HttpResult.ok(result);
    }
}
